package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.utils.SnowFlakeID;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.ArchivesClassify;
import com.tuocent.dagv2.entity.Task;
import com.tuocent.dagv2.mapper.ArchivesClassifyMapper;
import com.tuocent.dagv2.service.IArchivesClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuocent.dagv2.service.ITaskService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案门类 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-07-22
 */
@Service
public class ArchivesClassifyServiceImpl extends ServiceImpl<ArchivesClassifyMapper, ArchivesClassify> implements IArchivesClassifyService {

    @Resource
    private RedisCache redis;

    @Resource
    private SnowFlakeID snowFlakeID;

    @Autowired
    private ITaskService taskService;

    @Override
    public AjaxResult getClassifyData(Map<String, String> data) {
        QueryWrapper<ArchivesClassify> query = new QueryWrapper<>();
        query.eq("isdel", "0");
        for (String key : data.keySet()) {
            if (!Arrays.asList(PublicConst.DEF_FIELD).contains(key)) {
                if (!"".equals(data.get(key))) {
                    query.eq(key, data.get(key));
                }
            }
        }
        List<ArchivesClassify> list = list(query);
        return AjaxResult.success(list);
    }

    @Override
    public String setClassify(Model model, String id) {
        ArchivesClassify data = getById(id);
        if (data == null) {
            data = new ArchivesClassify();
        }
        model.addAttribute("data", data);
        return "/basic/setArchivesClassify";
    }

    @Override
    public AjaxResult saveClassifyData(ArchivesClassify data) {
        QueryWrapper<ArchivesClassify> qw = new QueryWrapper<>();
        qw.eq("classify_name", data.getClassifyName())
                .eq("isdel", 0);
        if ("".equals(data.getId())) {
            data.setId(snowFlakeID.getId());
            data.setIsdel("0");
        } else {
            qw.ne("id", data.getId());
        }
        if (count(qw) > 0) {
            return AjaxResult.error("该档案门类名称已存在");
        }
        return saveOrUpdate(data) ? AjaxResult.success("设置档案门类信息成功") : AjaxResult.error("设置档案门类信息失败");
    }

    @Override
    public AjaxResult delClassifyData(String id) {

        if (taskService.count(new QueryWrapper<Task>().eq("archives_classify_id", id).eq("isdel", 0)) > 0) {
            return AjaxResult.error("删除档案门类失败，该档案门类已在任务中使用");
        }

        UpdateWrapper<ArchivesClassify> updateWrapper = new UpdateWrapper<ArchivesClassify>()
                .set("isdel", "1")
                .eq("id", id);
        return update(updateWrapper) ? AjaxResult.success("删除档案门类成功") : AjaxResult.error("删除档案门类失败");
    }
}
